package com.collins.thread.practices.restaurant;

public class Producer implements Runnable{
    @Override
    public void run() {
        while(true){
            synchronized (Desk.lock){
                if(Desk.count == 0){
                    break;
                }else{
                    // consumer waiting, producer needs to produce
                    if(Desk.flag == 0){
                        Desk.count ++ ;
                        System.out.println("producer produce one new product, current stock is: "+ Desk.count);
                        Desk.flag = 1;
                        Desk.lock.notifyAll();
                        try {
                            Thread.sleep(100);
                            // Thread.yield();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }else{
                        try {
                            Desk.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
    }
}
